package com.epalmpay.controller.apiShopApp;

import com.epalmpay.commom.BaseController;
import com.epalmpay.dto.mall.EntityGoodInDTO;
import com.epalmpay.dto.mall.MallGoodInDTO;
import com.epalmpay.dto.mall.MallGoodListOutDTO;
import com.epalmpay.dto.mall.MallGoodQueryDTO;
import com.epalmpay.dto.order.MallOrderInDTO;
import com.epalmpay.dto.order.MallOrderQueryDTO;
import com.epalmpay.dto.order.OrderGoodOutDTO;
import com.epalmpay.dto.system.BaseOutDTO;
import com.epalmpay.entity.Page;
import com.epalmpay.exception.ApplicationException;
import com.epalmpay.service.app.IAppShopMallService;
import com.epalmpay.service.mall.IMallGoodService;
import com.epalmpay.service.order.IOrderGoodService;
import com.epalmpay.service.weixin.ITokenService;
import com.epalmpay.status.Rescode;
import com.epalmpay.util.Response;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.SimpleFormatter;

/**
 * Created by Administrator on 2018/2/9.
 */
@RestController
@RequestMapping(value="/app/shop/mall")
public class ShopMallApiController extends BaseController{

    @Resource
    private ITokenService tokenService;

    @Resource
    private IAppShopMallService appShopMallService;
    @Resource
    private IOrderGoodService orderGoodService;
    @Resource
    private IMallGoodService mallGoodService;

    @RequestMapping(value = "/orderList")
    public Response orderList(MallOrderQueryDTO mallOrderQueryDTO) throws Exception {
//        Boolean flag = tokenService.checkToken(mallOrderQueryDTO.getShopId().toString(), mallOrderQueryDTO.getToken());
//        if (!flag) {
//            return Response.getError(Response.LOGIN_CODE,"登录失效");
//        }
        try {
            Page page = new Page();
            page.setShowCount(mallOrderQueryDTO.getPageSize());
            page.setCurrentPage(mallOrderQueryDTO.getPageNo());
            Map<String, Object> data = new HashMap<>();
            Map<String, Long> statusCount = appShopMallService.shopOrderList(mallOrderQueryDTO.getShopId());
            List<OrderGoodOutDTO> orderGoodOutDTOList = appShopMallService.listMallOrder(mallOrderQueryDTO);
            data.put("orderList", orderGoodOutDTOList);
            data.put("statusCount", statusCount);
            return Response.getSuccess(data);
        } catch (ApplicationException ae) {
            logger.error(ae);
            ae.printStackTrace();
            return Response.getError(ae.errMsg);
        } catch (Exception e) {
            logger.error(e);
            e.printStackTrace();
            return Response.getError("服务器异常");
        }
    }

    @RequestMapping(value = "/orderDetail")
    public Response orderDetail(MallOrderQueryDTO mallOrderQueryDTO) throws Exception {

//        Boolean flag = tokenService.checkToken(mallOrderQueryDTO.getShopId().toString(), mallOrderQueryDTO.getToken());
//        if (!flag) {
//            return Response.getError(Response.LOGIN_CODE,"登录失效");
//        }
        try {
            OrderGoodOutDTO result = orderGoodService.get(mallOrderQueryDTO);
            return Response.getSuccess(result);
        } catch (Exception e) {
            logger.error(e);
            e.printStackTrace();
            return Response.getError("服务器异常");
        }
    }

    @RequestMapping(value = "/orderManipulation")
    public Response orderManipulation(MallOrderInDTO mallOrderInDTO) throws Exception {
//        Boolean flag = tokenService.checkToken(mallOrderInDTO.getShopId().toString(), mallOrderInDTO.getToken());
//        if (!flag) {
//            return Response.getError(Response.LOGIN_CODE,"登录失效");
//        }
        try {
            BaseOutDTO baseOutDTO = orderGoodService.saveManipulation(mallOrderInDTO,ROOT_HOME_URL);
            if(baseOutDTO.getErrorCode().equals(Rescode.SUCCESS)) {
                return Response.getSuccess("操作成功");
            } else {
                return Response.getError(baseOutDTO.getErrorMsg());
            }
        } catch (ApplicationException ae) {
            logger.error(ae);
            ae.printStackTrace();
            return Response.getError(ae.errMsg);
        } catch (Exception e) {
            logger.error(e);
            e.printStackTrace();
            return Response.getError("服务器异常");
        }
    }

    //商城商品 服务商品 列表

    @RequestMapping(value = "/entityMallGoodList")
    public Response listMallGood(@RequestParam Long shopId, Integer goodType, Byte type, Integer mode,Integer orderType,
                                 Integer pageNo, Integer pageSize,Byte auditStatus, String token, Long categoryFirstId,
                                 Long categorySecondId, String name) {
//        Boolean flag = tokenService.checkToken(mallGoodQueryDTO.getShopId().toString(), mallGoodQueryDTO.getToken());
//        if (!flag) {
//            return Response.getError(Response.LOGIN_CODE,"登录失效");
//        }
        MallGoodQueryDTO mallGoodQueryDTO = new MallGoodQueryDTO();
        mallGoodQueryDTO.setShopId(shopId);
        if(type != null) {
            mallGoodQueryDTO.setIsLaunch(type == 1 ? (byte)1 : (byte)0);
        }
        mallGoodQueryDTO.setPageNo(pageNo);
        mallGoodQueryDTO.setPageSize(pageSize);
        mallGoodQueryDTO.setAuditStatus(auditStatus);
        mallGoodQueryDTO.setCategoryFirstId(categoryFirstId);
        mallGoodQueryDTO.setCategorySecondId(categorySecondId);
        mallGoodQueryDTO.setName(name);
        try {
            MallGoodListOutDTO mallGoodListOutDTO;
            //商城商品
            if(goodType==1){
                 mallGoodListOutDTO = appShopMallService.listShopEntityGood(mallGoodQueryDTO, mode, orderType);
            }else{
                 mallGoodListOutDTO = appShopMallService.listShopMallGood(mallGoodQueryDTO, mode, orderType);
            }
            return Response.getSuccess(mallGoodListOutDTO);
        } catch (Exception e) {
            logger.error(e);
            e.printStackTrace();
            return Response.getError("服务器异常");
        }
    }

    //新增商城商品
    @RequestMapping(value = "/addMallGood")
    public Response addMallGood(EntityGoodInDTO entityGoodInDTO) {
//        Boolean flag = tokenService.checkToken(mallGoodInDTO.getShopId().toString(), mallGoodInDTO.getToken());
//        if (!flag) {
//            return Response.getError(Response.LOGIN_CODE,"登录失效");
//        }

        try {
            BaseOutDTO baseOutDTO = mallGoodService.saveEntityGoodAdd(entityGoodInDTO);
            if(baseOutDTO.getErrorCode().equals(Rescode.SUCCESS)) {
                return Response.getSuccess("添加成功");
            } else {
                return Response.getError("添加失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Response.getError("添加失败");
        }
    }
    //商城商品 服务商品上下 架

    @RequestMapping(value = "/shopMallGoodManipulation")
    public Response shopMallGoodManipulation(MallGoodInDTO mallGoodInDTO) {
//        Boolean flag = tokenService.checkToken(mallGoodInDTO.getShopId().toString(), mallGoodInDTO.getToken());
//        if (!flag) {
//            return Response.getError(Response.LOGIN_CODE,"登录失效");
//        }
        return appShopMallService.shopMallGoodManipulation(mallGoodInDTO);
    }

        @RequestMapping(value = "/addRemake")
    public Response addRemake(MallOrderQueryDTO mallOrderQueryDTO) {
        return appShopMallService.addRemark(mallOrderQueryDTO);
    }


    //新增服务商品
    @RequestMapping(value = "/addServiceMallGood")
    public Response addServiceMallGood(MallGoodInDTO mallGoodInDTO) throws ParseException {
//        Boolean flag = tokenService.checkToken(mallGoodInDTO.getShopId().toString(), mallGoodInDTO.getToken());
//        if (!flag) {
//            return Response.getError(Response.LOGIN_CODE,"登录失效");
//        }

        try {
            BaseOutDTO baseOutDTO = mallGoodService.saveServiceMallGood(mallGoodInDTO);
            if(baseOutDTO.getErrorCode().equals(Rescode.SUCCESS)) {
                return Response.getSuccess("添加成功");
            } else {
                return Response.getError("添加失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Response.getError("添加失败");
        }
    }


    /**
     *  商品详情
     * @param id
     * @param groupId
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/goodDetail", method = { RequestMethod.GET, RequestMethod.POST })
    @ResponseBody
    public Response goodDetail(String id,String groupId) throws Exception {
        logBefore(logger, "商品详情");
        HashMap<String, String> resultMap = new HashMap<String, String>();
        resultMap.put("商品id", id);
        String nullParam = Response.checkNull(resultMap);
        if (null != nullParam) {
            return Response.getError(nullParam + "不能为空");
        }
        try {
            Response response = appShopMallService.goodDetail(id,groupId);
            return response;
        } catch (ApplicationException ae) {
            logger.error(ae);
            ae.printStackTrace();
            return Response.getError(ae.errMsg);
        } catch (Exception e) {
            logger.error(e);
            e.printStackTrace();
            return Response.getError("服务器异常");
        }
    }




}
